package com.acwing.partition2;

import java.io.*;
import java.util.PriorityQueue;

/**
 * @author `RKC`
 * @date 2021/12/30 9:00
 */
@SuppressWarnings("all")
public class AC178第K短路 {

    private static final int N = 1010, M = 200010, INF = 0x3f3f3f3f;
    private static int n = 0, m = 0, index = 0;
    private static int[] head = new int[N], reverseHead = new int[N], element = new int[M], next = new int[M], weight = new int[M];
    private static boolean[] visited = new boolean[N];
    private static int[] visitCount = new int[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        for (int i = 0; i <= n; i++) head[i] = reverseHead[i] = -1;
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split("\\s+");
            int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]), w = Integer.parseInt(s[2]);
            createEdge(head, u, v, w);
            createEdge(reverseHead, v, u, w);
        }
        s = reader.readLine().split("\\s+");
        int start = Integer.parseInt(s[0]), end = Integer.parseInt(s[1]), k = Integer.parseInt(s[2]);
        writer.write(aStar(start, end, start == end ? k + 1 : k) + "\n");
        writer.flush();
    }

    private static int aStar(int start, int end, int k) {
        //A*算法需要使用到估价值，这个估价值可以通过dijkstra预处理得到终点到其它所有点的最短距离，这个最短距离作为估价函数
        int[] dist = dijkstra(end);
        //使用一个三元组来表示（真实值+估价值，真实值，当前点）
        PriorityQueue<int[]> heap = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[0], o2[0]));
        heap.add(new int[]{dist[start], 0, start});
        for (int i = 0; i <= n; i++) visitCount[i] = 0;
        while (!heap.isEmpty()) {
            int[] tuple = heap.poll();
            int real = tuple[1], u = tuple[2];
            visitCount[u]++;
            if (visitCount[end] == k) return real;
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = element[i];
                if (visitCount[v] >= k) continue;
                heap.add(new int[]{real + weight[i] + dist[v], real + weight[i], v});
            }
        }
        return -1;
    }

    private static int[] dijkstra(int end) {
        int[] dist = new int[N];
        for (int i = 0; i < N; i++) dist[i] = INF;
        PriorityQueue<int[]> heap = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[1], o2[1]));
        heap.add(new int[]{end, 0});
        dist[end] = 0;
        while (!heap.isEmpty()) {
            int[] tuple = heap.poll();
            int u = tuple[0], dis = tuple[1];
            if (visited[u]) continue;
            visited[u] = true;
            for (int i = reverseHead[u]; i != -1; i = next[i]) {
                int v = element[i];
                if (dis + weight[i] < dist[v]) {
                    dist[v] = dis + weight[i];
                    heap.add(new int[]{v, dist[v]});
                }
            }
        }
        return dist;
    }

    private static void createEdge(int[] head, int u, int v, int w) {
        element[index] = v;
        next[index] = head[u];
        head[u] = index;
        weight[index++] = w;
    }
}
